﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Xml;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Snowdreamist.Web.Rss;
using Snowdreamist.Web.Rss.V2;

namespace Snowdreamist.Test.Web.Rss
{
    /// <summary>
    /// RssTest 的摘要说明
    /// </summary>
    [TestClass]
    public class RssTest
    {
        public RssTest()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，该上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试特性
        //
        // 编写测试时，可以使用以下附加特性:
        //
        // 在运行类中的第一个测试之前使用 ClassInitialize 运行代码
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // 在类中的所有测试都已运行之后使用 ClassCleanup 运行代码
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // 在运行每个测试之前，使用 TestInitialize 来运行代码
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // 在每个测试运行完之后，使用 TestCleanup 来运行代码
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void RssV2CC98Top10Test()
        {
            // load test file
            using (FileStream fileStm = new FileStream(@"Rss\cc98_top10.xml", FileMode.Open, FileAccess.Read))
            using (var xmlreader = XmlReader.Create(fileStm))
            {
                Parser parser = new Parser();
                var feed = parser.Parse(xmlreader);
                // assert
                Assert.AreEqual<string>("CC98.org - 热门话题", feed.Channel.Title);
                Assert.AreEqual<string>("浙江大学CC98学生论坛", feed.Channel.Description);
                Assert.AreEqual<string>("http://www.cc98.org", feed.Channel.Link);
                Assert.AreEqual<int>(10, feed.Channel.Items.Count);
                // items data
                Item[] expectedItems = new Item[]
                {
                    new Item(){ Title=@"神器", Category=@"开怀一笑", Link =@"http://www.cc98.org/dispbbs.asp?boardid=135&id=3858260", Guid=@"http://www.cc98.org/dispbbs.asp?boardid=135&id=3858260", PublishDate = null },
                    new Item(){ Title=@"[复旦数学系77级毕业30周年聚会]   华丽丽的现任职名单看得跪下了", Category="校园信息", Link =@"http://www.cc98.org/dispbbs.asp?boardid=100&id=3857929", Guid=@"http://www.cc98.org/dispbbs.asp?boardid=100&id=3857929", PublishDate = null },
                    new Item(){ Title=@"[FEC]浙江大学未来企业家俱乐部2012春季诚意纳新（2月18日18:30公开麦秀）", Category=@"社团百花园", Link =@"http://www.cc98.org/dispbbs.asp?boardid=437&id=3857928", Guid=@"http://www.cc98.org/dispbbs.asp?boardid=437&id=3857928", PublishDate = null },
                    new Item(){ Title=@"[CIE]研究生创新创业中心2012年春季诚意纳贤！", Category=@"浙大研究生", Link =@"http://www.cc98.org/dispbbs.asp?boardid=581&id=3857865", Guid=@"http://www.cc98.org/dispbbs.asp?boardid=581&id=3857865", PublishDate = null },
                    new Item(){ Title=@"男朋友的情商低怎么办", Category=@"情感空气", Link =@"http://www.cc98.org/dispbbs.asp?boardid=81&id=3857980", Guid=@"http://www.cc98.org/dispbbs.asp?boardid=81&id=3857980", PublishDate = null },
                    new Item(){ Title=@"筑梦公益二〇一二春季纳新火热进行中~~~~（2月21日晚截止）", Category=@"公益爱心", Link =@"http://www.cc98.org/dispbbs.asp?boardid=571&id=3857801", Guid=@"http://www.cc98.org/dispbbs.asp?boardid=571&id=3857801", PublishDate = null },
                    new Item(){ Title=@"已出", Category=@"电脑数码", Link =@"http://www.cc98.org/dispbbs.asp?boardid=562&id=3858164", Guid=@"http://www.cc98.org/dispbbs.asp?boardid=562&id=3858164", PublishDate = null },
                    new Item(){ Title=@"在论坛上真能找到女朋友么", Category=@"缘分天空", Link =@"http://www.cc98.org/dispbbs.asp?boardid=152&id=3857960", Guid=@"http://www.cc98.org/dispbbs.asp?boardid=152&id=3857960", PublishDate = null },
                    new Item(){ Title=@"林书豪真不怎么样，你们不信", Category=@"篮球板块", Link =@"http://www.cc98.org/dispbbs.asp?boardid=86&id=3858131", Guid=@"http://www.cc98.org/dispbbs.asp?boardid=86&id=3858131", PublishDate = null },
                    new Item(){ Title=@"[系庆非官方]说好的卫衣来了！！！（小更新）", Category=@"不败光电", Link =@"http://www.cc98.org/dispbbs.asp?boardid=214&id=3857962", Guid=@"http://www.cc98.org/dispbbs.asp?boardid=214&id=3857962", PublishDate = null }
                };
                // assert items
                for (int i = 0; i < feed.Channel.Items.Count; ++i)
                    Assert.AreEqual<bool>(true, this.IsRssItemsEqual(feed.Channel.Items[i], expectedItems[i]));
            }
        }

        /// <summary>
        /// Compare two rss items
        /// Description won't be compared by default
        /// </summary>
        /// <param name="item1"></param>
        /// <param name="item2"></param>
        /// <returns></returns>
        private bool IsRssItemsEqual(Item item1, Item item2)
        {
            return item1.Title == item2.Title && item1.Category == item2.Category && item1.Link == item2.Link && item1.Guid == item2.Guid && item1.PublishDate == item2.PublishDate;
        }
    }
}
